/// Single Player Panel ///

using System.Windows.Forms;
using System.Drawing;
using System.Net;
using System.Threading;
using ajma.Utils;
using System;
using System.IO;
using System.Collections;
using System.Diagnostics;

namespace Xyne
{
	public class SinglePlayerPanel : Panel
	{
		private const string PLAYER_KEY = "player_name";
		private const string DECK_START_KEY = "deck_start";
		private const string DECK_END_KEY = "deck_end";
		private const string DECK_FOLDER = "decks/";
		private const string PLAYER_FILENAME = "player";
		private const int LIST_WIDTH = 175;
		private const int LIST_HEIGHT = 150;
		
		private Button cancel;
		private Button start;
		private Label deckLabel;
		private ListBox deckList;
		private ButtonClicked backCallback;
		private Hashtable decks = new Hashtable();
		
		public delegate void ButtonClicked();
		
		public ButtonClicked BackCallback
		{
			get { return backCallback; }
			set { backCallback = value; }
		}
		
		public SinglePlayerPanel()
		{
			ConstructControls();
			
			string[] files = Directory.GetFiles(DECK_FOLDER);
			
			foreach( string filename in files )
			{
				try
				{
					FileInfo file = new FileInfo(filename);
					StreamReader reader = file.OpenText();
					
					string deckName = reader.ReadLine();
					
					reader.Close();
					
					deckList.Items.Add(deckName);
					decks.Add(deckName, filename);
				}
				catch( Exception ) {}
			}
		}
		
		private void ConstructControls()
		{
			SuspendLayout();
			
			deckLabel = new Label();
			deckLabel.Text = "Deck";
			deckLabel.Location = new Point(5,5);
			Controls.Add(deckLabel);
			
			deckList = new ListBox();
			deckList.HorizontalScrollbar = true;
			deckList.Location = new Point(5,deckLabel.Location.Y + deckLabel.Height);
			deckList.Size = new Size(LIST_WIDTH,LIST_HEIGHT);
			Controls.Add(deckList);
			
			int width = deckList.Location.X + LIST_WIDTH + 10;
			int height = deckList.Location.Y + deckList.Height + 5;
			
			cancel = new Button();
			cancel.Text = "Cancel";
			cancel.Location = new Point(width - 15 - cancel.Size.Width,height);
			cancel.Click += new EventHandler(CancelClicked);
			Controls.Add(cancel);
			
			start = new Button();
			start.Text = "Start";
			start.Location = new Point(cancel.Location.X - start.Size.Width - 5,height);
			start.Click += new EventHandler(StartClicked);
			Controls.Add(start);
			
			height += cancel.Size.Height + 30;
			
			ClientSize = new Size(width,height);
			
			ResumeLayout();
		}
		
		private void CancelClicked(object sender, EventArgs e)
		{
			backCallback();
		}
		
		private void StartClicked(object sender, EventArgs e)
		{
			File.Delete(PLAYER_FILENAME);
			
			FileInfo file = new FileInfo(PLAYER_FILENAME);
			StreamWriter write = file.CreateText();
			
			write.WriteLine(PLAYER_KEY + " = " + Settings.GetSettings().PlayerName);
			
			write.WriteLine(DECK_START_KEY);
			
			FileInfo deck = new FileInfo(decks[deckList.SelectedItem.ToString()].ToString());
			StreamReader reader = deck.OpenText();
					
			reader.ReadLine(); // throw away deck name
			
			string line;
			while( (line = reader.ReadLine()) != null )
			{
				foreach( Pack pack in CardRepository.GetRepository().Packs )
					foreach( Card card in pack.Cards )
						if( line == card.Name )
						{
							write.WriteLine(card.Name);
							write.WriteLine(card.Description);
							foreach( string str in card.Pattern.Split('|') )
								write.WriteLine(str);
							foreach( Effect effect in card.Effects )
							{
								write.WriteLine(effect.ID);
								write.WriteLine(effect.ProcRate);
								write.WriteLine(effect.Enemy);
								write.WriteLine(effect.ShortText);
								write.WriteLine(effect.LongText);
								write.WriteLine(effect.Meta);
							}
						}
			}
			reader.Close();
			
			write.WriteLine(DECK_END_KEY);
			
			write.Close();
				
			Process process = new Process();
			process.StartInfo.FileName = "xyne";
			process.Start();
		}
	}
}